package com.emm.yixun.website.common.utils.tree;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.emm.yixun.common.model.Resources;


public class MenuTreeUtil {
	
	
	   
	/**
	 * 建立树菜单
	 * @param menusDB 菜单集合（不是树）
	 * @return 有样式的树的html字符串
	 */
    public static String buildTreeHtml(List<Resources> menusDB){  
    	StringBuffer html = new StringBuffer();
        for (int i=0;i<menusDB.size();i++) { 
        	Resources node=menusDB.get(i);
            if ("menu_0000".equals(node.getParentCode())) { 
            	boolean childrenHas=false;
            	List<Resources> children = getChildren(menusDB,node);  
            	if(!children.isEmpty())childrenHas=true;
            	if(i==0){
            		html.append("\r\n<li id='"+node.getResourcesCode()+"' class='active' >"); 
            	}else{
            		html.append("\r\n<li id='"+node.getResourcesCode()+"' >"); 
            	}           	
            	html.append("\r\n<a ");
            	html.append(" href='#maincontent' style='cursor:pointer;'  onclick=\"openMenu('"+node.getResourcesLevel()+"','"+node.getResourcesCode()+"','menu_0000','"+node.getResourcesName()+"','"+(StringUtils.isNotBlank(node.getResourcesHref())?node.getResourcesHref().trim():"noset")+"',this)\" ");       		    	
            	if(childrenHas)html.append(" target='mainFrame' class='dropdown-toggle' "); 
            	html.append(" >"); 
            	if(StringUtils.isNotEmpty(node.getResourcesIcon()))html.append("\r\n<i class= '"+ node.getResourcesIcon()+"' ></i>"); 
            	html.append("\r\n<span class='menu-text'>" + node.getResourcesName()+ "</span>"); 
            	if(childrenHas)html.append("<b class='arrow icon-angle-down'></b>");
            	html.append("</a>");
                build(menusDB,node,html);  
                html.append("</li>");
            }  
        }  
        return html.toString();  
    }  
      
    private static void build(List<Resources> menusDB,Resources node,StringBuffer html){  
        List<Resources> children = getChildren(menusDB,node); 
        if (!children.isEmpty()) {  
            html.append("\r\n<ul class='submenu'>");
            for (Resources child : children) {  
            	boolean childrenHas=false;
            	List<Resources> children2 = getChildren(menusDB,child);
            	if(!children2.isEmpty())childrenHas=true;
            	html.append("\r\n<li id='"+child.getId()+"'>"); 
            	html.append("\r\n<a ");
        		html.append(" href='#maincontent' style='cursor:pointer;' onclick=\"openMenu('"+child.getResourcesLevel()+"','"+child.getResourcesCode()+"','"+child.getParentCode()+"','"+child.getResourcesName()+"','"+(StringUtils.isNotBlank(child.getResourcesHref())?child.getResourcesHref().trim():"noset")+"',this)\" ");
            	if(childrenHas)html.append(" target='mainFrame' class='dropdown-toggle' "); 
            	html.append(" >"); 
            	if(!"0".equals(child.getParentCode()))html.append("\r\n<i class='icon-double-angle-right' ></i>"); 
            	html.append("\r\n<span class='menu-text'>");
            	if(StringUtils.isNotEmpty(child.getResourcesIcon())){
            		html.append("\r\n<i class= '"+ child.getResourcesIcon()+"' ></i>&nbsp;&nbsp;"+child.getResourcesName());
            	}else{
            		html.append(child.getResourcesName()); 
            	}
            	html.append("</span>"); 
            	if(childrenHas)html.append("<b class='arrow icon-angle-down'></b>");
            	html.append("</a>");
                build(menusDB,child,html);  
                html.append("</li>");  
            } 
            html.append("\r\n</ul>");
        }   
    }  
      
    private static List<Resources> getChildren(List<Resources> menusDB,Resources node){  
        List<Resources> children = new ArrayList<Resources>();  
        String nodeCode = node.getResourcesCode();  
        for (Resources child : menusDB) {  
            if (nodeCode.equals(child.getParentCode())) {  
                children.add(child);               
            }  
        }  
        return children;  
    }  
    
	   
	/**
	 * 建立树菜单
	 * @param menusDB 菜单集合（不是树）
	 * @return 有样式的树的菜单集合
	 */
/*    public static List<Resources> buildTree(List<Resources> menusDB){  
    	List<Resources> res=new ArrayList<Resources>();
    	  for (Resources node:menusDB) { 
    		  if ("menu_0000".equals(node.getResourcesCode())) { 
    			  List<Resources> children = getChildren(menusDB,node);  
    			  node.setNodes(children);
    			  build(menusDB,node,res); 
    			  res.add(node);
    		  }    		
    	  }
    	return res;
    }  
    
    private static void build(List<Resources> menusDB,Resources node,List<Resources> res){  
        List<Resources> children = getChildren(menusDB,node); 
        if (!children.isEmpty()) {  
            for (Resources child : children) {  
            	List<Resources> children2 = getChildren(menusDB,child);
            	child.setNodes(children2);
                build(menusDB,child,res);  
            } 
        }   
    } */
    
    
}
